//
//  CXDestinationCityListViewController.m
//  Travel
//
//  Created by dllo on 15/12/2.
//  Copyright © 2015年 doll. All rights reserved.
//

#import "CXDestinationCityListViewController.h"
#import "CXDestinationModel.h"
#import "CXDestinationDataModel.h"
#import "CXNetHandle.h"
#import "UIImageView+WebCache.h"
#import "CXDestinationDetailCollectionViewCell.h"
#import "CXDestinationDetailHeadCollectionReusableView.h"
#import "CXDestinationMissViewController.h"
#import "CXDestinationGoodViewController.h"
#import "CXDestinationDetailViewController.h"


@interface CXDestinationCityListViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,UIScrollViewDelegate>

@property (nonatomic, retain)UICollectionViewFlowLayout *layout;
@property (nonatomic, retain)UICollectionView *collectionView;

@property (nonatomic, retain) NSDictionary *dataDic;

//热门地点对象数组
@property (nonatomic, retain) NSArray *arr_model_site;

//头部图片
@property (nonatomic, retain) NSArray *arr_model_place;
@property (nonatomic, retain) UIImageView *backImg;

//国家名
@property (nonatomic, retain) UILabel *countryLabel;
//去过人数
@property (nonatomic, retain) UILabel *goCount;
//渐变色图片
//@property (nonatomic, retain)UIImageView *img;

@end

@implementation CXDestinationCityListViewController


- (void)dealloc
{
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self addScreenEdgePan];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[[UIImage imageNamed:@"leftBack"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(backToLast)];
    
    [self getData];
    
    [self createCollectionView];
    
    [CXLoadMBProgress loadMBProgress:self.view];

}

#pragma mark - 获取data
- (void)getData
{
    NSString *url;
    if (!self.model_data.typeId) {
        url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/", self.type, self.valueId];
    }else{
        url = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/place/%@/%@/", self.model_data.typeId, self.model_data.valueId];
    }
    [CXNetHandle getUrl:url cookie:nil result:^(id result) {
        
        if (result != nil) {
             [CXLoadMBProgress removeMBProgress:self.view];
        }
       
        self.dataDic = result;
        
        self.arr_model_site = [CXDestinationDataModel transformWithArray:result[@"hottest_sites"]];
        
        self.arr_model_place = [CXDestinationDataModel transformWithArray:result[@"hottest_places"]];
        
        if (self.arr_model_place.count != 0) {
            [self.backImg sd_setImageWithURL:[NSURL URLWithString:[self.arr_model_place[0] photo]] placeholderImage:[UIImage imageNamed:@"load"]];
        }
        
        self.goCount.text = [NSString stringWithFormat:@"%@去过/%@想去", self.dataDic[@"visited_count"], self.dataDic[@"wish_to_go_count"]];

        
        [self.collectionView reloadData];
        
    }];
    
    
}
#pragma mark - 创建collectionView
- (void)createCollectionView
{
    self.layout = [[UICollectionViewFlowLayout alloc]init];
    self.layout.itemSize = CGSizeMake((WIDTH - 30) / 2, (WIDTH - 30) / 2);
    self.layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.layout.headerReferenceSize = CGSizeMake(WIDTH, 20 + (WIDTH - 15) / 4 + 60);
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT) collectionViewLayout:self.layout];
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.93 alpha:1];
    
    self.collectionView.contentInset = UIEdgeInsetsMake(264, 0, 0, 0);
    
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
    
    [self.view addSubview:self.collectionView];
    
    //collectionView的cell
    [self.collectionView registerClass:[CXDestinationDetailCollectionViewCell class] forCellWithReuseIdentifier:@"destinationDetail"];
    
    //collectionView的头
    [self.collectionView registerClass:[CXDestinationDetailHeadCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    
    
    //设置tableView的contentView距离上边界264
    self.backImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, -264, WIDTH, 264)];
    
    
    self.backImg.contentMode = UIViewContentModeScaleAspectFill;//设置img高度改变时 宽度也改变
    //裁剪
    self.backImg.clipsToBounds = YES;
    [self.collectionView addSubview:self.backImg];
    
    
    //渐变色图片
    //    self.img = [[UIImageView alloc]initWithFrame:CGRectMake(0, -264, WIDTH, 264)];
    //    self.img.userInteractionEnabled = NO;
    //    self.img.image = [UIImage imageNamed:@"jiabian2"];
    //    [self.collectionView addSubview:self.img];
    
    self.countryLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, -80, 300 , 40)];
    [self.collectionView addSubview:self.countryLabel];
    self.countryLabel.textColor = [UIColor colorWithRed:1 green:0.98 blue:1 alpha:1];
    self.countryLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:26];
    self.countryLabel.text = self.dataDic[@"name"];
    
    self.goCount = [[UILabel alloc]initWithFrame:CGRectMake(20, -30 ,300 , 20)];
    self.goCount.textColor = [UIColor colorWithRed:1 green:0.98 blue:1 alpha:1];
    self.goCount.shadowColor = [UIColor colorWithRed:0.75 green:0.82 blue:0.76 alpha:1];
    
    [self.collectionView addSubview:self.goCount];
    
    
    //创建返回按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(13, 25, 30, 30);
    [self.view addSubview:btn];
    [btn setImage:[UIImage imageNamed:@"leftBack"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(backToLast) forControlEvents:UIControlEventTouchUpInside];
    
}


- (void)backToLast
{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark - 背景图下滑改变宽高
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //获取当前偏移量
    CGFloat y = scrollView.contentOffset.y;
    if (y < - 264) {
        CGRect frame = self.backImg.frame;
        frame.origin.y = y; //image 的frame不断往上偏移
        frame.size.height = -y;//collectionview向下偏移了多少 高度就增加多少
        self.backImg.frame = frame;
        //     self.img.frame = frame; //渐变色图片
    }
    //bar 将要出来时  显隐度一点点改变
    if (scrollView.contentOffset.y <= 0 && scrollView.contentOffset.y >= -64) {
        self.navigationController.navigationBar.alpha = (64 + scrollView.contentOffset.y) / 64;
        self.navigationController.navigationBar.hidden = NO;
    }
    else if (scrollView.contentOffset.y > 0)
    {
        self.navigationController.navigationBar.alpha = 1.0;
        
    }
    else
    {
        self.navigationController.navigationBar.alpha = 0.0;
    }
    
    
}

#pragma mark - 视图要消失时候 设置bar出现
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.alpha = 1.0;
    self.navigationController.navigationBar.hidden = NO;
}
#pragma mark - 视图要出现时候 隐藏

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear: animated];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.alpha = 0.0;
    self.navigationController.navigationBar.hidden = YES; //如果不写 从其他页过来 会出现一个navagationBar
    self.tabBarController.tabBar.hidden = YES;
    
}

#pragma mark - collection 的 header
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    CXDestinationDetailHeadCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
    
    void(^MISS)(NSString *) = ^(NSString *str_url){
        CXDestinationMissViewController *missVC = [[CXDestinationMissViewController alloc] init];
        missVC.str_url = str_url;
        [self.navigationController pushViewController:missVC animated:YES];
    };
    
    void(^MISSGOOD)(CXDestinationModel *) = ^(CXDestinationModel *m){
        CXDestinationGoodViewController *goodVC = [[CXDestinationGoodViewController alloc] init];
        goodVC.model_good = self.model_data;
        [self.navigationController pushViewController:goodVC animated:YES];
    };
    
    headerView.arr_tools = self.dataDic[@"tools"];
    headerView.miss = MISS;
    headerView.missGood = MISSGOOD;
    
    return headerView;
    
}


#pragma mark - collctionView 行数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.arr_model_site.count;
}

#pragma mark - collectionView cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CXDestinationDetailCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"destinationDetail" forIndexPath:indexPath];
    cell.model_destination = self.arr_model_site[indexPath.item];
    cell.hasStar = NO;
    
    return cell;
}
#pragma mark - collectionView 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CXDestinationDetailViewController *detailCityVC = [[CXDestinationDetailViewController alloc] init];
    detailCityVC.model_data = self.arr_model_site[indexPath.item];
    
    [self.navigationController pushViewController:detailCityVC animated:YES];
    
    
    
    

}
#pragma mark-滑动手势1
- (void)addScreenEdgePan
{
    UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(presentLeftMenuViewController:)];
    screenEdgePan.edges = UIRectEdgeLeft;
    [self.view addGestureRecognizer:screenEdgePan];
    [self.collectionView addGestureRecognizer:screenEdgePan];
}
#pragma mark-滑动手势2
-(void)presentLeftMenuViewController:(UIScreenEdgePanGestureRecognizer *)edge
{
    [self.navigationController popViewControllerAnimated:YES];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
